package com.taptech.doufu.presenter.home;
import android.app.Activity;
import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.taptech.doufu.constant.HomePageType;
import com.taptech.doufu.listener.CommmonImp;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.util.CacheUtil;
import com.taptech.doufu.bean.MarketNovelNoticeBean;
import com.taptech.doufu.bean.base.BaseResponseListBean;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;

import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.util.List;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Wyatt on 2017/10/13/013.
 */

public class HomeSpeakerPresenter {

    public interface OnHomeSpeakerView{
        void onSpeakerNext(List<MarketNovelNoticeBean> list,boolean isCache);
    }

    private OnHomeSpeakerView onHomeSpeakerView;
    private Context mContext;

    public HomeSpeakerPresenter(Context context,OnHomeSpeakerView onHomeSpeakerView) {
        this.onHomeSpeakerView = onHomeSpeakerView;
        mContext = context;
    }



    public void loadSpeakerCache(final String sexType) {
        Observable.create(new Observable.OnSubscribe<List<MarketNovelNoticeBean>>() {
                    @Override
                    public void call(Subscriber<? super List<MarketNovelNoticeBean>> subscriber) {
                        List<MarketNovelNoticeBean> list = null;
                        try {
                            JSONObject json = CacheUtil.readJsonDataFromCache(new File(CacheUtil.getMarketCachePathBySexType(sexType)));
                            list = com.alibaba.fastjson.JSONArray.parseArray(json.getJSONArray("list").toString(), MarketNovelNoticeBean.class);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        subscriber.onNext(list);
                    }
                })
                .compose(RxJavaHelper.<List<MarketNovelNoticeBean>>observeOnMainThread((Activity) mContext))
                .subscribe(new BaseSubscriber<List<MarketNovelNoticeBean>>(){
                    @Override
                    public void onNext(List<MarketNovelNoticeBean> listNotice) {
                        super.onNext(listNotice);
                        if (onHomeSpeakerView != null && listNotice!=null) {
                            onHomeSpeakerView.onSpeakerNext(listNotice,true);
                        }
                    }
                });
    }

    /**
     * 获取小喇叭数据
     */
    public void getHomeNovelSpeaker(final String sexType,boolean isUseCache) {
        ApiClient.getInstance().setUseCache(isUseCache).setSexType(sexType).getService().getNovelSpeaker(sexType)
                .map(new Func1<BaseResponseListBean<MarketNovelNoticeBean>, List<MarketNovelNoticeBean>>() {
                    @Override
                    public List<MarketNovelNoticeBean> call(BaseResponseListBean<MarketNovelNoticeBean> marketNovelNoticeBeanBaseResponseListBean) {
                        CacheUtil.saveJsonDataToCache(new Gson().toJson(marketNovelNoticeBeanBaseResponseListBean.getData()),new File(CacheUtil.getMarketCachePathBySexType(sexType)));
                        JsonArray topicData = new Gson().toJsonTree(marketNovelNoticeBeanBaseResponseListBean.getData().getList()).getAsJsonArray();
                        return com.alibaba.fastjson.JSONArray.parseArray(topicData.toString(), MarketNovelNoticeBean.class);
                    }
                })
                .compose(RxJavaHelper.<List<MarketNovelNoticeBean>>observeOnMainThread((Activity) mContext))
                .subscribe(new BaseSubscriber<List<MarketNovelNoticeBean>>() {
                    @Override
                    public void onNext(List<MarketNovelNoticeBean> list) {
                        super.onNext(list);
                        if (onHomeSpeakerView != null) {
                            onHomeSpeakerView.onSpeakerNext(list,true);
                        }
                    }
                });

    }

}
